# $NetBSD: Makefile,v 1.27 2015/07/14 19:44:09 fhajny Exp $

DISTNAME=		riak-2.1.4
CATEGORIES=		databases
MASTER_SITES=		http://s3.amazonaws.com/downloads.basho.com/riak/2.1/${PKGVERSION}/ \
			http://s3.amazonaws.com/files.basho.com/solr/ \
			http://s3.amazonaws.com/files.basho.com/yokozuna/

MAINTAINER=		pkgsrc-users@NetBSD.org
HOMEPAGE=		http://www.basho.com/products_riak_overview.php
COMMENT=		Distributed, highly available data store
LICENSE=		apache-2.0

SOLR=			solr-4.7.0-yz-1.tgz
YOKOZUNA=		yokozuna-3.jar
YOKOMONIT=		yz_monitor-1.jar

DIST_SUBDIR=		basho
DISTFILES=		${DEFAULT_DISTFILES} ${SOLR} ${YOKOZUNA} ${YOKOMONIT}
EXTRACT_ONLY=		${DEFAULT_DISTFILES} ${SOLR}
EXTRACT_DIR.${SOLR}=	${WRKSRC}/deps/yokozuna/build

USE_LANGUAGES=		c c++
USE_LIBTOOL=		yes
USE_TOOLS+=		bash:run gmake

MAKE_JOBS_SAFE=		no

.include "../../mk/bsd.prefs.mk"

.include "../../wip/erlang-basho/Makefile.versions"
.include "Makefile.versions"

.for depname depver in ${RIAK_VERSIONS}
VERSION.${depname}=	${depver}
PLIST_SUBST+=		VERSION.${depname}=${depver}
PRINT_PLIST_AWK+=	{if ($$0 ~ /\/$(depname)-$(depver)\//) {sub(/\/$(depname)-$(depver)\//,"/$(depname)-$${VERSION.$(depname)}/", $$0);}}
.endfor

BUILD_DEFS+=		VARBASE RIAK_USER RIAK_GROUP
BUILD_DEFS+=		RIAK_DATA RIAK_LOG

RIAK_USER?=		riak
RIAK_GROUP?=		${RIAK_USER}
RIAK_DATA?=		${VARBASE}/db/riak
RIAK_LOG?=		${VARBASE}/log/riak
RIAK_RUN?=		${VARBASE}/run

PKG_GROUPS+=		${RIAK_GROUP}
PKG_USERS+=		${RIAK_USER}:${RIAK_GROUP}
PKG_HOME.${RIAK_USER}=	${RIAK_DATA}
PKG_GECOS.${RIAK_USER}=	Riak daemon user
PKG_SHELL.${RIAK_USER}=	${SH}

OWN_DIRS+=		${RIAK_DATA} ${RIAK_LOG}
OWN_DIRS_PERMS+=	${RIAK_DATA} ${RIAK_USER} ${RIAK_GROUP} 0770
OWN_DIRS_PERMS+=	${RIAK_LOG} ${RIAK_USER} ${RIAK_GROUP} 0770

PKG_SYSCONFSUBDIR=	riak
CONF_FILES+=		share/examples/riak/riak.conf ${PKG_SYSCONFDIR}/riak.conf
CONF_FILES+=		share/examples/riak/solr-log4j.properties ${PKG_SYSCONFDIR}/solr-log4j.properties

BUILD_TARGET=		rel

SUBST_CLASSES+=		pkgsrc
SUBST_STAGE.pkgsrc=	pre-build
SUBST_MESSAGE.pkgsrc=	Fixing pkgsrc locations and tools
SUBST_FILES.pkgsrc=	deps/node_package/priv/base/env.sh rel/vars.config
SUBST_FILES.pkgsrc+=	deps/cuttlefish/rebar.config
SUBST_VARS.pkgsrc=	PREFIX PKG_SYSCONFDIR PKGVERSION_NOREV
SUBST_VARS.pkgsrc+=	RIAK_USER RIAK_DATA RIAK_LOG RIAK_RUN

REPLACE_BASH+=		deps/eper/priv/bin/*
REPLACE_BASH+=		deps/yokozuna/build/${SOLR:S/.tgz//}/example/etc/create-solrtest.keystore.sh

REPLACE_INTERPRETER+=	escript
REPLACE.escript.old=	.*escript
REPLACE.escript.new=	${PREFIX}/bin/escript
REPLACE_FILES.escript=	deps/node_package/priv/base/nodetool

CHECK_PORTABILITY_SKIP=	deps/riak_search/tests/riak_search/run_all.sh

MAKE_ENV+=		PATCH=${PATCH:Q}
MAKE_ENV+=		REBAR_OPTS=escript_shebang='\#!/opt/local/bin/escript\n'

INSTALL_ENV+=		REPO=riak
INSTALL_ENV+=		PKG_VERSION=${PKGVERSION_NOREV}
INSTALL_ENV+=		OSNAME=${OPSYS}
INSTALL_ENV+=		ARCH=${MACHINE_ARCH}

FILES_SUBST+=		ERTS_VERSION=${VERSION.erts}
FILES_SUBST+=		RIAK_USER=${RIAK_USER}
FILES_SUBST+=		RIAK_GROUP=${RIAK_GROUP}
FILES_SUBST+=		RIAK_DATA=${RIAK_DATA}

# Helper target to regenerate Makefile.versions
update-deps: build
	(${ECHO} '# $$NetBSD$$'; \
	 ${ECHO} '# This file is generated by "${MAKE} update-deps", post-build'; \
	 ${ECHO}; \
	 cd ${WRKSRC}/deps && \
	 ${GREP} vsn */ebin/*.app */apps/*/ebin/*.app | \
	 ${SED} -e 's|^.*/\(.*\).app:.*"\(.*\)"},|RIAK_VERSIONS+=\1 \2|' | \
	 ${SORT}; ) \
	 > ${.CURDIR}/../../wip/riak/Makefile.versions

post-extract:
	${MKDIR} ${WRKSRC}/deps/yokozuna/priv/java_lib
	${MKDIR} ${WRKSRC}/deps/yokozuna/priv/solr/lib/ext
	${CP} ${_DISTDIR}/${YOKOZUNA} ${WRKSRC}/deps/yokozuna/priv/java_lib
	${CP} ${_DISTDIR}/${YOKOMONIT} ${WRKSRC}/deps/yokozuna/priv/solr/lib/ext
	${INSTALL_DATA} ${FILESDIR}/nspr-src-configure2.patch \
	  ${WRKSRC}/deps/erlang_js/c_src/patches
	${CHMOD} -R u=rwX,g=rX,o=rX ${WRKSRC}
	${CHOWN} -R ${ROOT_USER}:${ROOT_GROUP} ${WRKSRC}

do-install:
	${RM} -f ${WRKSRC}/rel/riak/lib/runtime_tools-${VERSION.runtime_tools}/priv/obj/dtrace_user.o
	${MKDIR} ${WRKSRC}/deps/node_package/priv/templates/pkgsrc
	${INSTALL_DATA} ${FILESDIR}/Makefile \
	  ${WRKSRC}/deps/node_package/priv/templates/pkgsrc
	cd ${WRKSRC}/deps/node_package/priv/templates/pkgsrc && \
	   ${SETENV} ${INSTALL_ENV} ${MAKE_ENV} \
	   RIAK_PATH=${WRKSRC} VERSION_STRING=${PKGVERSION_NOREV} \
           ${MAKE_PROGRAM} ${MAKE_FLAGS} ${INSTALL_MAKE_FLAGS}

.include "../../devel/ncurses/buildlink3.mk"
BUILDLINK_DEPMETHOD.erlang-basho=	build
.include "../../wip/erlang-basho/buildlink3.mk"
.include "../../mk/bsd.pkg.mk"
